const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
require("express-async-errors");

const app = express();

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));

// 设置跨域和相应数据格式
app.all("/api/*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With, mytoken");
  res.header("Access-Control-Allow-Headers", "X-Requested-With, Authorization");
  res.setHeader("Content-Type", "application/json;charset=utf-8");
  res.header(
    "Access-Control-Allow-Headers",
    "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"
  );
  res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By", " 3.2.1");
  if (req.method == "OPTIONS") res.sendStatus(200);
  /*让options请求快速返回*/ else next();
});

const resextra = require("./modules/resextra");
app.use(resextra);

const loginRouter = require("./routes/login");
app.use("/api/v1/login", loginRouter);

const { tokenAuth } = require("./modules/passport");
app.use(tokenAuth);

const menusRouter = require("./routes/menus");
app.use("/api/v1/menus", menusRouter);

const authPublicRouter = require("./routes/authPublic");
app.use("/api/v1/auth", authPublicRouter);

const authorization = require("./modules/authorization");
authorization.invocation("/api/v1");
app.use(authorization.checkAuth);

const rightRouter = require("./routes/root/right");
app.use("/api/v1/root/right", rightRouter);

const roleRouter = require("./routes/root/role");
app.use("/api/v1/root/role", roleRouter);

const facultiesRouter = require("./routes/admin/faculties");
app.use("/api/v1/admin/faculties", facultiesRouter);

const specializedRouter = require("./routes/admin/specialized");
app.use("/api/v1/admin/specialized", specializedRouter);

const classRouter = require("./routes/admin/class");
app.use("/api/v1/admin/class", classRouter);

const studentRouter = require("./routes/admin/student");
app.use("/api/v1/admin/student", studentRouter);

const teacherRouter = require("./routes/admin/teacher");
app.use("/api/v1/admin/teacher", teacherRouter);

const courseRouter = require("./routes/admin/course");
app.use("/api/v1/admin/course", courseRouter);

const chooseRouter = require("./routes/admin/choose");
app.use("/api/v1/admin/choose", chooseRouter);

const adminRouter = require("./routes/admin/admin");
app.use("/api/v1/admin/admin", adminRouter);

const statsRouter = require("./routes/admin/stats");
app.use("/api/v1/admin/stats", statsRouter);

const instructionRouter = require("./routes/teacher/instruction");
app.use("/api/v1/tec/instruction", instructionRouter);

const scheduleRouter = require("./routes/teacher/schedule");
app.use("/api/v1/tec/schedule", scheduleRouter);

const sourceRouter = require("./routes/teacher/source");
app.use("/api/v1/tec/source", sourceRouter);

const teacherInfoRouter = require("./routes/teacher/info");
app.use("/api/v1/tec/info", teacherInfoRouter);

const studentScheduleRouter = require("./routes/student/schedule");
app.use("/api/v1/stu/schedule", studentScheduleRouter);

const studentSelectRouter = require("./routes/student/select");
app.use("/api/v1/stu/select", studentSelectRouter);

const studentInfoRouter = require("./routes/student/info");
app.use("/api/v1/stu/info", studentInfoRouter);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
  res.sendResult(null, 404, "访问的地址不存在");
});

// error handler
app.use(function (err, req, res, next) {
  console.log(err);
  if (err.name === "UnauthorizedError") {
    res.sendResult(null, 401, "无效token,请求先登录");
  } else {
    res.sendResult(null, 500, "服务器错误");
  }
});

const all_routes = require("express-list-endpoints");
all_routes(app).forEach((item) => {
  global.service_rights.push(item.path);
});

module.exports = app;
